var Sequelize = require('sequelize')
var env       = process.env.NODE_ENV || 'development'
var conf      = require('../config/database')[env]

const sequelize = new Sequelize(conf)

sequelize.authenticate().then(() => {
  log.success('数据库连接成功')
}).catch(err => {
  log.error('数据库连接失败', err)
  process.exit(0)
})


var User = sequelize.import('../models/user')
var Log = sequelize.import('../models/log')
var Player = sequelize.import('../models/player')
var Config = sequelize.import('../models/config')


Log.belongsTo(User, {as: 'oper', foreignKey: 'uid'})


// var Auth = sequelize.import('../models/auth')
// var Post = sequelize.import('../models/post')
// var Tag = sequelize.import('../models/tag')

// var Role = sequelize.import('../models/role')
// var RoleUser = sequelize.import('../models/role-user')
// var Permission = sequelize.import('../models/permission')
// var PermissionRole = sequelize.import('../models/permission-role')

// Post.belongsTo(User, {as: 'author', foreignKey: 'uid'})
// Post.belongsTo(Tag, {as: 'tagType', foreignKey: 'tid'})

// RoleUser.belongsTo(Role, {foreignKey: 'rid'})
// RoleUser.belongsTo(User, {foreignKey: 'uid'})
// User.hasMany(RoleUser, {foreignKey: 'uid'})


module.exports = sequelize
